package com.biwin.core.main.runner;

import com.biwin.common.api.constant.BwSystemConstant;
import org.springframework.beans.BeansException;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import java.io.IOException;
import java.lang.reflect.Method;

/**
 * TODO
 *
 * @author biwin
 * @since 0.0.1
 */
@Configuration
public class BwDefaultRunnerListener implements ApplicationRunner, ApplicationContextAware {
    private ApplicationContext applicationContext;

    @Override
    public void run(ApplicationArguments args) {
        // 主动打开浏览器,并访问项目主页
        try {
            runBrowser(checkOS());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private OS checkOS() {
        String osName = System.getProperty("os.name", "");
        if (osName.startsWith(BwSystemConstant.OS_WINDOW)) {
            return OS.Window;
        } else if (osName.startsWith(BwSystemConstant.OS_MAC)) {
            return OS.MacOS;
        } else {
            return OS.Linux;
        }
    }

    private void runBrowser(OS type) throws Exception {
        Environment env = applicationContext.getEnvironment();
        String url = "http://localhost:" + env.getProperty("server.port") + "/doc.html";
        switch (type) {
            case Window:
                OpenUrl4Window(url);
                break;
            case MacOS:
                OpenUrl4Mac(url);
                break;
            default:
                OpenUrl4Linux(url);
                break;
        }
    }

    private void OpenUrl4Window(String url) throws IOException {
        Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
    }

    private void OpenUrl4Mac(String url) throws Exception {
        Class fileMgr = Class.forName("com.apple.eio.FileManager");
        Method openURL = fileMgr.getDeclaredMethod("openURL", String.class);
        openURL.invoke(null, url);
    }

    private void OpenUrl4Linux(String url) throws Exception {
        String[] browsers = {"firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape"};
        String browser = null;
        // 执行代码，在brower有值后跳出，
        for (int count = 0; count < browsers.length && browser == null; count++) {
            // 这里是如果进程创建成功了，==0是表示正常结束。
            if (Runtime.getRuntime().exec(new String[]{"which", browsers[count]}).waitFor() == 0) {
                browser = browsers[count];
            }
        }

        if (browser == null) {
            throw new RuntimeException("未找到任何可用的浏览器");
        } else {// 这个值在上面已经成功的得到了一个进程。
            Runtime.getRuntime().exec(new String[]{browser, url});
        }

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    enum OS {
        Window,
        MacOS,
        Linux;
    }
}
